﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Homework1.ImageryService;
using Homework1.GeocodeService;


namespace Homework1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private string GeocodeAddress(string address)
        {
            string results = "";
            string key = "";
            GeocodeRequest geocodeRequest = new GeocodeRequest();

            // Set the credentials using a valid Bing Maps key
            geocodeRequest.Credentials = new GeocodeService.Credentials();
            geocodeRequest.Credentials.ApplicationId = key;

            // Set the full address query
            geocodeRequest.Query = address;

            // Set the options to only return high confidence results 
            ConfidenceFilter[] filters = new ConfidenceFilter[1];
            filters[0] = new ConfidenceFilter();
            filters[0].MinimumConfidence = GeocodeService.Confidence.High;

            // Add the filters to the options
            GeocodeOptions geocodeOptions = new GeocodeOptions();
            geocodeOptions.Filters = filters;
            geocodeRequest.Options = geocodeOptions;

            // Make the geocode request
            GeocodeServiceClient geocodeService = new GeocodeServiceClient("BasicHttpBinding_IGeocodeService");
            GeocodeResponse geocodeResponse = geocodeService.Geocode(geocodeRequest);

            if (geocodeResponse.Results.Length > 0)
            {
                double lat = geocodeResponse.Results[0].Locations[0].Latitude;
                double lon = geocodeResponse.Results[0].Locations[0].Longitude;
                results = lat + ";" + lon;
            }
            else
                results = null;
            
            return results;
        }

        private string GetImagery(string locationString)
        {
            string key = "";
            MapUriRequest mapUriRequest = new MapUriRequest();

            // Set credentials using a valid Bing Maps key
            mapUriRequest.Credentials = new ImageryService.Credentials();
            mapUriRequest.Credentials.ApplicationId = key;

            // Set the location of the requested image
            mapUriRequest.Center = new ImageryService.Location();
            string[] digits = locationString.Split(';');
            mapUriRequest.Center.Latitude = double.Parse(digits[0].Trim(), System.Globalization.NumberStyles.Any);
            mapUriRequest.Center.Longitude = double.Parse(digits[1].Trim(), System.Globalization.NumberStyles.Any);

            // Set the map style and zoom level
            MapUriOptions mapUriOptions = new MapUriOptions();
            mapUriOptions.Style = MapStyle.AerialWithLabels;
            mapUriOptions.ZoomLevel = 17;

            // Set the size of the requested image in pixels
            mapUriOptions.ImageSize = new ImageryService.SizeOfint();
            mapUriOptions.ImageSize.Height = 500;
            mapUriOptions.ImageSize.Width = 520;

            mapUriRequest.Options = mapUriOptions;

            //Make the request and return the URI
            ImageryServiceClient imageryService = new ImageryServiceClient("BasicHttpBinding_IImageryService");
            MapUriResponse mapUriResponse = imageryService.GetMapUri(mapUriRequest);
            return mapUriResponse.Uri;
        }

        private void RequestImageLocation_Click(object sender, RoutedEventArgs e)
        {
            string coords = GeocodeAddress(textInput.Text);
            if (coords != null)
            {
                // Make image visible
                imageResults.Visibility = Visibility.Visible;

                //Get URI and set image
                String imageURI = GetImagery(coords);
                imageResults.Source = new BitmapImage(new Uri(imageURI));
            }
            else
            {
                imageResults.Visibility = Visibility.Hidden;
                labelResults.Content = "No Results Found";
            }
        }

        private void RequestImageCoords_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (double.Parse(latBox.Text) > -85 &&
                        double.Parse(latBox.Text) < 85 &&
                        double.Parse(lonBox.Text) > -180 &&
                        double.Parse(lonBox.Text) < 180)
                {
                    // Make image visible
                    imageResults.Visibility = Visibility.Visible;

                    //Get URI and set image
                    String imageURI = GetImagery(latBox.Text + ";" + lonBox.Text);
                    imageResults.Source = new BitmapImage(new Uri(imageURI));
                }
                else
                {
                    imageResults.Visibility = Visibility.Hidden;
                    labelResults.Content = "Invalid Coordinates";
                }
            }
            catch (FormatException)
            {
                imageResults.Visibility = Visibility.Hidden;
                labelResults.Content = "Invalid Coordinates";
            }
        }
    }
}
